package com.ruoyi.back.service.impl;

import java.util.*;

import com.ruoyi.back.domain.OrderDetail;
import com.ruoyi.back.mapper.OrderDetailMapper;
import com.ruoyi.back.service.IOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单详情Service业务层处理
 * 
 * @author luguanqing
 * @date 2021-04-21
 */
@Service
public class OrderDetailServiceImpl implements IOrderDetailService {
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 查询订单详情
     *
     * @param id 订单详情ID
     * @return 订单详情
     */
    @Override
    public OrderDetail selectOrderDetailById(String id) {
        return orderDetailMapper.selectOrderDetailById(id);
    }

    /**
     * 查询订单详情列表
     *
     * @param orderDetail 订单详情
     * @return 订单详情
     */
    @Override
    public List<OrderDetail> selectOrderDetailList(OrderDetail orderDetail) {
        return orderDetailMapper.selectOrderDetailList(orderDetail);
    }

    /**
     * 新增订单详情
     *
     * @param orderDetail 订单详情
     * @return 结果
     */
    @Override
    public int insertOrderDetail(OrderDetail orderDetail) {
        return orderDetailMapper.insertOrderDetail(orderDetail);
    }

    /**
     * 修改订单详情
     *
     * @param orderDetail 订单详情
     * @return 结果
     */
    @Override
    public int updateOrderDetail(OrderDetail orderDetail) {
        return orderDetailMapper.updateOrderDetail(orderDetail);
    }

    /**
     *
     * @param orderStatus, orderNo 订单详情
     * @param orderNo
     * @return
     */
    @Override
    public int updateOrderStatus(String orderStatus, String orderNo) {
        return orderDetailMapper.updateOrderStatus(orderStatus, orderNo);
    }

    /**
     * 批量删除订单详情
     *
     * @param ids 需要删除的订单详情ID
     * @return 结果
     */
    @Override
    public int deleteOrderDetailByIds(String[] ids) {
        return orderDetailMapper.deleteOrderDetailByIds(ids);
    }

    /**
     * 删除订单详情信息
     *
     * @param id 订单详情ID
     * @return 结果
     */
    @Override
    public int deleteOrderDetailById(String id) {
        return orderDetailMapper.deleteOrderDetailById(id);
    }


    /**
     * 微信订单
     *
     * @param request
     * @param response
     * @throws Exception
     */
//    private void doWx(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        String number = request.getParameter("number") == null ? "" : request.getParameter("number");
//        OrderDetail p = orderDetailMapper.selectOrderDetailById(number);
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//        String timeStart = sdf.format(date);
//        Calendar cal = Calendar.getInstance();
//        cal.add(Calendar.DAY_OF_MONTH, 1);
//        Date date1 = cal.getTime();
//        String timeExpire = sdf.format(date1);
//        SortedMap<String, String> parameters = new TreeMap<String, String>();
//        parameters.put("appid", ConfigUtil.APPID);
//        parameters.put("body", p.getSpName());
//        parameters.put("mch_id", ConfigUtil.MCH_ID);
//        parameters.put("out_trade_no", number);
//        parameters.put("spbill_create_ip", CommonUtil.getIp(request));
//        DecimalFormat df = new DecimalFormat("#");
//        parameters.put("total_fee", df.format(Double.parseDouble(p.getTotalMoney().toString()) * 1000/10));
//        parameters.put("trade_type", "NATIVE");
//        parameters.put("time_expire", DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, DateUtils.addMilliseconds(p.getOrderTime(), 5 * 60 * 1000)));//二维码过期时间5分钟
//        parameters.put("nonce_str", WXPayUtil.generateNonceStr());
//        parameters.put("notify_url", ConfigUtil.NOTIFY_URL);//支付成功后回调的action，与JSAPI相同
//        String generateSignature = WXPayUtil.generateSignature(parameters, ConfigUtil.API_KEY, WXPayConstants.SignType.MD5);
//        parameters.put("sign", generateSignature);
//        String generateSignedXml = WXPayUtil.generateSignedXml(parameters, ConfigUtil.API_KEY);
//        System.out.println("微信支付预下单请求xml格式：：" + generateSignedXml);
//        String result = CommonUtil.httpsRequest(ConfigUtil.UNIFIED_ORDER_URL, "POST", generateSignedXml);
//        System.out.println(result);
//        Map<String, String> map;
//        try {
//
//            map = WXPayUtil.xmlToMap(result);
//            String returnCode = map.get("return_code");
//            String resultCode = map.get("result_code");
//            if (returnCode.equalsIgnoreCase("SUCCESS") && resultCode.equalsIgnoreCase("SUCCESS")) {
//                String codeUrl = map.get("code_url");
//                //TODO 拿到codeUrl，写代码生成二维码
//                System.out.println("codeUrl=" + codeUrl);
//                int width = 300;
//                int height = 300;
//                //二维码的图片格式
//                String format = "JPEG";
//                Hashtable hints = new Hashtable();
//                //内容所使用编码
//                hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//                BitMatrix bitMatrix = new MultiFormatWriter().encode(codeUrl,
//                        BarcodeFormat.QR_CODE, width, height, hints);
//                // response.setContentType("image/JPEG");
//                MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());
//
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
